﻿#region Using Directives

    using System;
    using System.Collections.Generic;

#endregion

namespace MarketPulse.BusinessLogic.Legacy
{
    #region AnswerOption

        /// <summary>
        /// Class used to represent a AnswerOption item
        /// </summary>
        public class AnswerOption
        {

            #region Constructors

                /// <summary>
                /// Default constructor for this class
                /// </summary>
                public AnswerOption()
                {
                }

                /// <summary>
                /// Overloaded constructor for this class (accepts the parameters needed to setup the object)
                /// </summary>
                /// <param name="id">The ID for this answer option</param>
                /// <param name="text">The text for this answer option</param>
                /// <param name="score">The score for this answer option</param>
                /// <param name="hasComment">Whether a comment is required for this answer option</param> 
                /// <param name="dependantQuestion">The ID's for questions that depend on this answer option being selected</param>
                public AnswerOption(int id, string text, double score, bool hasComment, int dependant)
                {
                    this.ID = id;
                    this.Text = text;
                    this.Score = score;
                    this.HasComment = hasComment;
                    this.Dependant = dependant;
                }

            #endregion

            #region Properties

                /// <summary>The ID for this AnswerOption</summary>
                public int ID { get; set; }

                /// <summary>The value for this AnswerOption</summary>
                public string Text { get; set; }

                /// <summary>The score available for this AnswerOption</summary>
                public double Score { get; set; }

                /// <summary>Whether this Answer option has a comment field available</summary>
                public bool HasComment { get; set; }

                /// <summary>The comment associated with this question</summary>
                public string Comment { get; set; }

                /// <summary>The ID of the questions dependant on this answer option</summary>
                public int Dependant { get; set; }

                /// <summary>Whether this option is a response (selected or filled in etc...)</summary>
                public bool IsResponse { get; set; }

            #endregion
        }

    #endregion

    #region AnswerOptionCollection

        /// <summary>
        /// Class used to represent a collection of AnswerOption items
        /// </summary>
        public class AnswerOptionCollection : List<AnswerOption>
        {

            #region Methods

                /// <summary>
                /// Method used to find a specific answer option based on the ID supplied
                /// </summary>
                /// <param name="id">The ID of the answer option to find</param>
                /// <returns>A valid answer option object</returns>
                public AnswerOption Option(int id)
                {
                    AnswerOption temporaryAnswerOption = this.Find(delegate(AnswerOption option)
                    {
                        return (option.ID == id);
                    });
                    return temporaryAnswerOption;
                }

            #endregion

            #region Properties

                /// <summary>The combined score for all answer options (marked as reponses) in the list</summary>
                public double CombinedScore
                {
                    get
                    {
                        double combinedScore = 0.0;

                        //// Loop through the collection and calculate the combined score
                        //Enumerator answerOptionEnumerator = this.GetEnumerator();

                        //while (answerOptionEnumerator.MoveNext())
                        //{
                        //    AnswerOption temporaryAnswerOption = answerOptionEnumerator.Current;
                        //    if(temporaryAnswerOption.IsResponse)
                        //    {
                        //        combinedScore += temporaryAnswerOption.Score;
                        //    }
                        //}

                        this.ForEach(delegate(AnswerOption option)
                        {
                            if (option.IsResponse)
                            {
                                combinedScore += option.Score;
                            }
                        });

                        return combinedScore;
                    }
                }

                /// <summary>Whether or not all answer options were selected as responses</summary>
                public bool AllAreResponses
                {
                    get
                    {
                        bool allResponses = false;

                        // Loop through the collection and look for any options that weren't selected, exit if you find any
                        Enumerator answerOptionEnumerator = this.GetEnumerator();

                        while (answerOptionEnumerator.MoveNext())
                        {
                            AnswerOption temporaryAnswerOption = answerOptionEnumerator.Current;
                            if (!temporaryAnswerOption.IsResponse)
                            {
                                break;
                            }
                            else
                            {
                                allResponses = true;
                            }
                        }
                        return allResponses;
                    }
                }

                /// <summary>Whether or not any answer options are selected</summary>
                public bool AnyAreResponses
                {
                    get
                    {
                        bool anyResponses = true;

                        // Loop through the collection and look for any options that were selected, exit if you find any
                        Enumerator answerOptionEnumerator = this.GetEnumerator();

                        while (answerOptionEnumerator.MoveNext())
                        {
                            AnswerOption temporaryAnswerOption = answerOptionEnumerator.Current;
                            if (temporaryAnswerOption.IsResponse)
                            {
                                anyResponses = true;
                                break;
                            }
                            else
                            {
                                anyResponses = false;
                            }
                        }
                        return anyResponses;
                    }
                }

            #endregion
        }

    #endregion        
}